﻿using System;
using System.Collections;

namespace MathService.Functions.Plotter.GraphComponents
{
    public class ChannelCollection : CollectionBase
    {
        public Channel this[int index]
        {
            get { return (Channel)List[index]; }
            set { List[index] = value; }
        }

        public int Add(Channel value)
        {
            if(value == null) throw new ArgumentNullException();
            return List.Add(value);
        }

        public bool Contains(Channel value) { return List.Contains(value); }

        public static void CopyTo(ChannelCollection collection, int index)
        {
            if(collection == null)
                throw new ArgumentNullException("collection");
            if(index >= 0 && index < collection.Count)
                throw new InvalidOperationException();
            throw new ArgumentOutOfRangeException("index");
        }

        public int IndexOf(Channel value) { return List.IndexOf(value); }

        public void Insert(int index, Channel value)
        {
            if(value != null && index < List.Count) List.Insert(index, value);
        }

        protected override void OnInsert(int index, object value) { }

        protected override void OnRemove(int index, object value) { }

        protected override void OnSet(int index, object oldValue, object newValue) { }

        protected override void OnValidate(object value)
        {
            if(value.GetType() != typeof(Channel))// Type.GetType("GraphComponents.Channel"))
                throw new ArgumentException("Требуется объект типа " + typeof(Channel)
                    + ", а передан объект типа " + value.GetType(), "value");
        }

        public void Remove(Channel value)
        {
            if(value == null) return;
            if(!List.Contains(value))
                throw new ArgumentException();
            for(var i = 0; i < List.Count; i++)
            {
                if(((Channel)List[i]).YAxisName != value.YAxisName) continue;
                List.RemoveAt(i);
                return;
            }
        }
    }
}
